package JavaSE.MSB.Thread.C09_AQS;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class T03_Sync extends AbstractQueuedSynchronizer
{
    @Override
    protected boolean tryAcquire(int arg)
    {
        if (compareAndSetState(0, 1))
        {
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }
        return false;
    }
    
    @Override
    protected boolean tryRelease(int arg)
    {
        setExclusiveOwnerThread(null);
        setState(0);
        return true;
    }
    
    @Override
    protected boolean isHeldExclusively()
    {
        return getState() == 1;
    }
}
